#include "alg.h"
#include "assert.h"

#include <fstream>

using namespace boost;
using namespace boost::numeric::ublas;
using namespace std;


void Gauss(Mat& A, int r, int s)
{
    int rows = A.size1();
    int cols = A.size2();
    assert(0<=r && r<rows);
    assert(0<=s && s<cols);

    assert(A(r,s)!=Q(0));
    Q Ars = A(r,s);
    for(int j = 0;j<cols;j++)
    {
        A(r,j) = A(r,j)/Ars;
    }
    for(int i = 0;i<rows;i++)
    {
        if(i!=r)
        {
            Q Ais = A(i,s);
            for(int j = 0;j<cols;j++)
            {
                A(i,j) = A(i,j)-A(r,j)*Ais;
            }
        }
    }
}

bool Load(Mat& A, string filename)
{
    fstream f;
    f.open(filename.c_str(), ios_base::in);
    if(!f.is_open())
    {
        return false;
    }

    int rows, cols;
    f >> rows >> cols;
    A.resize(rows, cols, false);
    for(int i = 0;i<rows;i++)
    {
        for(int j = 0;j<cols;j++)
        {
            int value;
            f >> value;
            A(i,j) = Q(value);
        }
    }

    f.close();
    return true;
}


void Print(Mat& A, std::ostream& out)
{
    int rows = A.size1();
    int cols = A.size2();

    for(int i = 0;i<rows;i++)
    {
        for(int j = 0;j<cols;j++)
        {
            Q a = A(i,j);
            if(a.denominator()==1)
            {
                out << "\t" << a.numerator();
            }
            else
            {
                out << "\t" << A(i,j);
            }
        }
        out << endl;
    }
}
